package com.jec.module;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.google.common.base.StandardSystemProperty;
import com.jec.Bootstrap;
import com.jec.module.business.dao.RecordRepository;
import com.jec.module.business.entity.Record;
import com.jec.module.sysmanage.dao.SysLogRepository;
import com.jec.module.sysmanage.entity.OperateLog;
import com.jec.module.sysmanage.service.SysLogService;
import com.jec.utils.Directory;

import lombok.extern.log4j.Log4j;

@Component
@Log4j
public class StoreGuard {
	
    @Autowired
    private SysLogRepository sysLogRepository;
    
    @Autowired
    private RecordRepository recordRepository;
    
	@Autowired
	private SysLogService sysLogService;
	
    @Scheduled(cron="0 0/30 *  * * ? ")  
	public void guardOperateLogs() {
		
		Calendar day = Calendar.getInstance();
		day.add(Calendar.DAY_OF_MONTH, -109);
		day.set(Calendar.HOUR_OF_DAY, 0);
		day.set(Calendar.MINUTE, 0);
		day.set(Calendar.SECOND, 0);
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		log.info("Syslog guard date: " + dateFormat.format(day.getTime()));
		
		List<OperateLog> logs = sysLogRepository.findAllByCreateTimeBefore(day.getTime());
		sysLogRepository.delete(logs);

	}
	
    @Scheduled(cron="0 0/30 *  * * ? ")
	public void guardPlatformLogs() {
		Directory d = Directory.byUserDir("tcs_logs");
		d.guardMaxFileCount(30);
	}
    
    @Scheduled(cron="0 0/30 *  * * ? ")
	public void guardRecords() {
    	
    	Properties app = Bootstrap.getApplicationProperties();
    	
    	String limitString = app.getProperty("record.limit", "10G");
    	
    	long limit = parseCapacity(limitString);
    	
    	String path = app.getProperty("record.path");
    	if(path == null || path.isEmpty()) {
    		path = StandardSystemProperty.USER_DIR.value() + File.separator + "audio";
    	} 
    	
    	Directory recordDir = Directory.byDirectly(path);
    	long size = recordDir.getSize();
    	
    	if(size > limit) {
    		
    		// 记录到日志
    		sysLogService.addLog(0, "2", "录音文件超过预分配空间，系统将自动清理");
    		
    		Calendar day = Calendar.getInstance();
    		day.add(Calendar.DAY_OF_MONTH, -90);
    		day.set(Calendar.HOUR_OF_DAY, 0);
    		day.set(Calendar.MINUTE, 0);
    		day.set(Calendar.SECOND, 0);
    		
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    		log.info("Record guard date: " + dateFormat.format(day.getTime()));
    		
    		List<Record> records = recordRepository.findAllByStartTimeBefore(day.getTime());
    		for(Record r : records) {
    			Directory.deleteAllFilesOfDir(new File(r.getFilePath()));
    			recordRepository.delete(r);
    		}
    		
    	}
    	
	}
    
    private long parseCapacity(String value) {
    	
    	long k = 1;
    	
    	if(StringUtils.endsWithIgnoreCase(value, "k")) {
    		k = k * 1024;
    		return k * parseCapacity(value.substring(0, value.length() - 1));
    	} else if(StringUtils.endsWithIgnoreCase(value, "M")) {
    		k = k * 1024 * 1024;
    		return k * parseCapacity(value.substring(0, value.length() - 1));
    	} else if(StringUtils.endsWithIgnoreCase(value, "G")) {
    		k = k * 1024 * 1024 * 1024;
    		return k * parseCapacity(value.substring(0, value.length() - 1));
    	} else {
    		return Long.parseLong(value);
    	}
    	
    }

}
